package io.renren.common.utils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 天眼查
 * https://openapi.qcc.com/manage/data/interface
 */
@Component
public class TianyanUtil {

    private static String token = "e68bc4e0-eb16-466f-9074-a09a361eb86e";
    //特殊企业基本信息
    private static String url = "https://open.api.tianyancha.com/services/open/ic/baseinfo/special?keyword=";

    /**
     * 主函数
     *
     * @param args
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // token可以从 数据中心 -> 我的接口 中获取
//        boolean flag = isCompany("杭州市余杭区海辰小学", "12330110MB1G382403");
        boolean flag = isCompany("开化县钱学森学校", "12330824MBIL93658M");
        //boolean flag = isCompany("艾思能達（香港）有限公司");
        System.out.println(flag);
    }

    /**
     * 验证企业和组织机构代码证是否匹配
     * @param keyword
     * @return
     */
    public static boolean isCompany(String keyword){
        JSONObject company = getCompanyList(keyword);
        if(company!=null) {
            if (company.get("name").equals(keyword)) {
                return true;
            }
        }
        return false;
    }


    /**
     * 验证企业和组织机构代码证是否匹配
     * @param keyword
     * @param companyNo
     * @return
     */
    public static boolean isCompany(String keyword, String companyNo){
        JSONObject company = getCompanyList(keyword);
        if(company!=null) {
            if (company.get("name").equals(keyword)
                    &&
                    (
                            companyNo.toUpperCase().equals(company.getStr("usCreditCode"))
                            || companyNo.toUpperCase().equals(company.getStr("creditCode"))
                    )
            ) {
                return true;
            }
        }
        return false;
    }

    //获取企业列表
    public static JSONObject getCompanyList(String keyword) {
        String result = executeGet(url + keyword, token);
        JSONObject jsonObject = JSONUtil.parseObj(result);
        if ("ok".equals(jsonObject.getStr("reason"))) {
            return (JSONObject) jsonObject.get("result");
        } else {
            System.out.println("调用天眼查接口失败");
            System.out.println(result);
            return null;
        }
    }

    /**
     * http get请求
     *
     * @param url   接口url
     * @param token token
     * @return 返回接口数据
     */
    protected static String executeGet(String url, String token) {
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 1000);
        HttpConnectionParams.setSoTimeout(httpParams, 1000);
        HttpClient httpClient = new DefaultHttpClient(httpParams);
        String result = null;
        try {
            HttpGet get = new HttpGet(url);
            // 设置header
            get.setHeader("Authorization", token);
            // 设置类型
            HttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            result = EntityUtils.toString(entity, "utf-8");

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httpClient.getConnectionManager().shutdown();
        }
        return result;
    }

}
